package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/role")
public class RoleController {
	
	@Autowired
	private IRoleService roleService;

	@Autowired
	private IPermissionService permissionService;

	@RequestMapping("list")
	public String list(Model model, @ModelAttribute("qo")QueryObject qo){
		model.addAttribute("result", roleService.query(qo));
		return "/role/list";
	}
	@RequestMapping("/input")
	public String input(Long id,Model model){
		if(id != null){
			model.addAttribute("entity",roleService.selectByPrimaryKey(id));
		}
		model.addAttribute("allExpression",permissionService.selectAll());
		return "/role/input";
	}
	@RequestMapping("/saveOrUpdate")
	public String saveOrUpdate(Role entity,Long[] ids){
		if(entity.getId() != null){
			roleService.updateByPrimaryKey(entity,ids);
		}else{
			roleService.insert(entity,ids);
		}
		
		return "redirect:/role/list.do";
	}
	@RequestMapping("/delete")
	@ResponseBody
	public Object saveDelete(Long id){
		JSONResult result = new JSONResult();
		try {
			if(id != null){
				roleService.deleteByPrimaryKey(id);
			}

		}catch (Exception e){
			e.printStackTrace();
			result.mark(e.getMessage());
		}

		return result;
	}
}
